<?php

namespace App\Service\Index;

use App\Cache\InviteVideoCache;
use App\Cache\RedisKey;
use App\Constants\InviteVideoCode;
use App\Helpers\RedisUtil;
use App\Model\CallBase;
use App\Model\CloseFriend;
use App\Model\InviteVideoLog;
use App\Model\Message;
use App\Model\PayOrder;
use App\Model\UserBlacklist;
use App\Model\VoiceCall;
use App\Model\CallHangup;
use App\Model\VideoCall;
use App\Service\BaseService;
use App\Service\ServiceTrait\GetUserInfoTrait;
use App\Service\ServiceTrait\InviteVideoCheckTrait;

/**
 * 挂断(呼叫中, 未接通)
 */
class HangupService extends BaseService
{
    use InviteVideoCheckTrait,GetUserInfoTrait;

    protected function RoVerifyList()
    {
        return [
            'chat_user_id' => 'required|int',
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
        $chatUserId = $this->request->input('chat_user_id', 0);
        //通话类型：1-视频，0-默认语音
        $callType = $this->request->input('call_type', 0);

        //通话模型
        $mainModel = $callType == 1 ? VideoCall::class : VoiceCall::class;
        //查找记录
        $record = $mainModel::checkRecord($userId, $chatUserId);
        $ret['result'] = 1;
        //非呼叫中的状态, 不处理
        if (!$record || $record->status != CallBase::STATUS_NO_PICK) {
            // log_user_result($userId, "CallStatus:{$record->status}==CallId:{$record->id}==CallType:{$callType}");
            return $ret;
        }
        //如果是被叫方挂断
        if ($record->chat_user_id == $userId) {
            //当前一键语聊或者新手语聊
            switch ($record->from){
                case CallBase::FROM_NEW:
                case CallBase::FROM_QUCIK:
                    Message::sendSlient($record->user_id, 'CHANGE_FOR_QUICK');
                    break;
            }
//            if ($record->from == CallBase::FROM_QUCIK) {
//                Message::sendSlient($record->user_id, 'CHANGE_FOR_QUICK');
//            } elseif ($record->from == CallBase::FROM_NEW) {
//                Message::sendSlient($record->user_id, 'CHANGE_FOR_NEW');
//            }
        }


        if ($record->from == CallBase::FROM_INVITE_CALL){
            if ($record->chat_user_id == $userId){
//                $this->hangUpInviteVideo($userId);

                $maleUid = $userId;
                $femaleUid = $chatUserId;
                $status = InviteVideoLog::STATUS_MALE_REFUSE;
            }else{
                $maleUid = $chatUserId;
                $femaleUid = $userId;
                $status = InviteVideoLog::STATUS_FEMALE_REFUSE;
            }

            $this->reloadData($maleUid,$femaleUid,$status);
//            //先解锁
//            InviteVideoCache::delLock($femaleUid,0);
//            InviteVideoCache::delLock($maleUid,1);
//
//            //更新状态
//            InviteVideoLog::updateStatus($maleUid, $femaleUid, $status);
//
//            //删除两者今天的逻辑关系
//            InviteVideoCache::delInviteRefuse($maleUid,$femaleUid);
//
//            //重新进队列
//            $this->runInvite($femaleUid);
//            $this->runInvite($maleUid);
        }

        //当前记录状态修改
        $status = $userId == $record->user_id ? CallBase::STATUS_CALLER_STOP : CallBase::STATUS_HOST_REJECT;
        $record->update(['status' => $status]);

        //修改挂断人之前状态
        $mainModel::finishOldCall($userId);

        //添加挂断记录
        CallHangup::addRecord($userId, $chatUserId, $record, $callType);

        log_user_result($userId, "ChatUser:{$chatUserId}==CallId:{$record->id}==CallType:{$callType}");
        return $ret;
    }
}
